package com.pingan.haofang.searchcloud.api.query.parser.dbobject.util;

import java.util.List;

import com.pingan.haofang.searchcloud.api.query.parser.dbobject.Condition;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.Operator;
import com.pingan.haofang.searchcloud.api.query.parser.dbobject.SqlNode;

/**
 * ClassName: ConditionUtil <br>
 * Function: 查询条件工具
 *
 * @author luyi
 * @version 1.0.0
 * @since 1.0.0
 * @date 20150515
 */
public class ConditionUtil {

    /**
     * 判断查询条件是否为join
     * 
     * @param condition
     * @param isInner 是否为内连接
     * @return
     */
    public static boolean isJoin(Condition condition, boolean isInner) {
        SqlNode left = condition.getLeft();
        List<SqlNode> rightList = condition.getRightList();

        if (isInner) {
            return condition.getOperator() == Operator.EQ && rightList.size() == 1 && SqlNodeUtil.isColumn(left)
                    && SqlNodeUtil.isColumn(rightList.get(0));
        }

        return SqlNodeUtil.containColumn(left) && SqlNodeUtil.containColumn(rightList);
    }

    /**
     * 判断查询条件是否为join
     * 
     * @param condition
     * @return
     */
    public static boolean isJoin(Condition condition) {
        return isJoin(condition, false);
    }

    /**
     * 判断查询条件是否为内连接
     * 
     * @param condition
     * @return
     */
    public static boolean isInnerJoin(Condition condition) {
        return isJoin(condition, true);
    }

}
